/*
 * Copyright 2012 Faculty of Informatics - Masaryk University.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.pa165.creatures.dto.mapping;

import cz.muni.fi.pa165.creatures.dto.CreatureTypeWeaponDTO;
import cz.muni.fi.pa165.creatures.dto.WeaponDTO;
import cz.muni.fi.pa165.creatures.dto.mapping.api.MappingDTO;
import cz.muni.fi.pa165.creatures.model.CreatureTypeWeapon;
import cz.muni.fi.pa165.creatures.model.Weapon;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * This class represents the mapping between
 * {@link cz.muni.fi.pa165.creatures.model.Weapon} and
 * {@link cz.muni.fi.pa165.creatures.dto.WeaponDTO} and vice versa.
 *
 * @author smikloso
 */
public class WeaponMapping implements MappingDTO<WeaponDTO, Weapon> {

    @Autowired
    private CreatureTypeWeaponMapping creatureTypeMapping;

    @Override
    public WeaponDTO entityToDTO(Weapon weapon) {
        WeaponDTO dto = new WeaponDTO();
        dto.setId(weapon.getId().toString());
        dto.setRange(weapon.getRange().toString());
        dto.setAmmunition(weapon.getAmmunition().toString());
        dto.setName(weapon.getName());
        if (weapon.getCreatureTypes() != null && !weapon.getCreatureTypes().isEmpty()) {
            List<CreatureTypeWeaponDTO> creatureTypeWeapons = new ArrayList<CreatureTypeWeaponDTO>();
            for (CreatureTypeWeapon creatureType : weapon.getCreatureTypes()) {
                creatureTypeWeapons.add(creatureTypeMapping.entityToDTO(creatureType));
            }
            dto.setCreatureTypeWeapons(creatureTypeWeapons);
        }
        return dto;
    }

    @Override
    public Weapon DTOtoEntity(WeaponDTO dto) {
        Weapon weapon = new Weapon();
        weapon.setId(Long.parseLong(dto.getId()));
        weapon.setName(dto.getName());
        weapon.setRange(Integer.parseInt(dto.getRange()));
        weapon.setAmmunition(Integer.parseInt(dto.getAmmunition()));
        if (dto.getCreatureTypeWeapons() != null && !dto.getCreatureTypeWeapons().isEmpty()) {
            List<CreatureTypeWeapon> creatureTypeWeapons = new ArrayList<CreatureTypeWeapon>();
            for (CreatureTypeWeaponDTO creatureTypeWeaponDTO : dto.getCreatureTypeWeapons()) {
                creatureTypeWeapons.add(creatureTypeMapping.DTOtoEntity(creatureTypeWeaponDTO));
            }
            weapon.setCreatureTypes(creatureTypeWeapons);
        }
        return weapon;
    }
}
